//
//  AFReachability.m
//  TasteEnjoy
//
//  Created by XuRui on 16/5/10.
//  Copyright © 2016年 XuRui. All rights reserved.
//

#import "AFReachability.h"
#import "AFNetworkReachabilityManager.h"
#import "AppDelegate.h"
@implementation AFReachability

+ (void)reachability
{

    
    [[self alloc]creat];
    
    
    
}

- (void)creat
{
   // 1.获得网络监控的管理者
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    
    // 2.设置网络状态改变后的处理
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        // 当网络状态改变了, 就会调用这个block
        switch (status) {
                
            case AFNetworkReachabilityStatusUnknown: // 未知网络
            {
                [self showStateMessage:@"亲, 你正在使用未知网络"];
                
                break;
            }
            case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
            {
                
                [self showStateMessage:@"当前无网络"];
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
            {
                
                [self showStateMessage:@"亲, 你正在使用流量"];
                
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
            {
                [self showStateMessage:@"当前是WIFI"];
                break;
            }
        }
    }];
    
    // 3.开始监控
    [mgr startMonitoring];

}
- (void)showStateMessage:(NSString *)state
{
     AppDelegate *app = [[UIApplication sharedApplication] delegate];
    UIAlertController *aletr = [UIAlertController alertControllerWithTitle:@"提示" message:state preferredStyle:UIAlertControllerStyleAlert];
    [app.window.rootViewController presentViewController:aletr animated:YES completion:^{
        
    }];
    // 提示框延迟消失
    [self performSelector:@selector(dismiss) withObject:aletr afterDelay:3];
}

- (void)dismiss{
     AppDelegate *app = [[UIApplication sharedApplication] delegate];
    [app.window.rootViewController dismissViewControllerAnimated:YES completion:^{
        
    }];
}

@end
